package servlet;

import dao.PinglunDao;
import dao.UserDao;
import javabean.Pinglun;
import javabean.User;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet("/pinglun")
public class PinLunServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PinglunDao dao=new PinglunDao();
        String method=request.getParameter("method");
        if(method.equals("list")){//展示所有评论
            try {
                List<Pinglun> pinglunList=dao.QueryAll();
                request.getSession().setAttribute("pinglunList",pinglunList);
                RequestDispatcher rd=request.getRequestDispatcher("denglu_chenggong.jsp");
                rd.forward(request,response);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else if(method.equals("add")){//新增评论
            String content=request.getParameter("content");
            String xm=(String)request.getSession().getAttribute("xm");
            Pinglun pinglun=new Pinglun();
            pinglun.setContent(content);
            pinglun.setName(xm);
            pinglun.setCtime(new Date());
            try {
                dao.add(pinglun);
                response.sendRedirect("/pinglun?method=list");
            } catch (Exception e) {
                e.printStackTrace();
            }

        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           doPost(request,response);
    }
}
